import java.io.*;
import java.util.HashSet;
import java.util.Set;

public class FileControl {
    public static void main(String[] args) {
        getProjectByCity("北京");
        System.out.println("**********************************************");
        getSupplier("红", "北京");
        System.out.println("**********************************************");
        System.out.println(addSupplier("S1", "J2", "P1"));
    }

    private static String addSupplier(String sno, String jno, String pno) {
        String filePath = "src/main/data/Part";
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            boolean isFirstLine = true; // 跳过标题行
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String[] data = line.split(",");
                if (pno.equals(data[0])) {
                    if (Integer.parseInt(data[3]) < 0) System.out.println("库存不足,无法提供");
                    String line1 = "\n" + data[0] + "," + data[1] + "," + data[2] + ","
                            + (Integer.parseInt(data[3]) - 1) + "\n";
                    try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true))) {
                        bw.write(line1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    addSupplies(sno, jno, pno);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "添加成功";
    }

    private static void addSupplies(String sno, String jno, String pno) {
        String line1 = "\n"+sno + "," + jno + "," + pno+"\n";
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/data/Supplies",
                true))) {
            // 在每行的开头插入字符
            bw.write(line1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void getSupplier(String color, String city) {
        String partsFilePath = "src/main/data/Part";
        String projectsFilePath = "src/main/data/Project";
        String suppliesFilePath = "src/main/data/Supplies";
        Set<String> redParts = new HashSet<>();
        Set<String> beijingProjects = new HashSet<>();
        Set<String> suppliers = new HashSet<>();
        try (BufferedReader br = new BufferedReader(new FileReader(partsFilePath))) {
            String line;
            boolean isFirstLine = true;
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String[] data = line.split(",");
                if (color.equals(data[2])) {
                    redParts.add(data[0]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (BufferedReader br = new BufferedReader(new FileReader(projectsFilePath))) {
            String line;
            boolean isFirstLine = true;
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String[] data = line.split(",");
                if (city.equals(data[2])) {
                    beijingProjects.add(data[0]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try (BufferedReader br = new BufferedReader(new FileReader(suppliesFilePath))) {
            String line;
            boolean isFirstLine = true;
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String[] data = line.split(",");
                //这里必须提供零件供应关系，项目供应关系
                if (redParts.contains(data[2]) && beijingProjects
                        .contains(data[1])) {
                    suppliers.add(data[0]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (String supplier : suppliers) {
            System.out.println("供应商编号: " + supplier);
        }
    }

    public static void getProjectByCity(String city) {
        String filePath = "src/main/data/Project";
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            boolean isFirstLine = true; // 跳过标题行
            while ((line = br.readLine()) != null) {
                if (isFirstLine) {
                    isFirstLine = false;
                    continue;
                }
                String[] data = line.split(",");
                if (city.equals(data[2])) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
